﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2010 ThinkWide
+-------------------------------------------------------------
项目: MFCommandBuilder数据库服务类


版本: 1


创建日期:2010-5-3


作者: 章称

+-------------------------------------------------------------
描述:z

更新:   2010-7-3

ToDo:

***************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MF.Core
{
    public class MFCommandBuilder
    {
        public DataTable Table { get; set; }
        public Type ClassType { get; set; }

        public string OriginalKey { get; set; }
        public string ParameterKey { get; set; }

        public MFCommandBuilder(DataTable dataTable)
        {
            Table = dataTable;
            ParameterKey = "@";
            OriginalKey = "Original_";
        }

        public MFCommandBuilder(Type type)
        {
            ClassType = type;
            ParameterKey = "@";
            OriginalKey = "Original_";
        }

        public string GetSelectSQL()
        {
            string sFields = string.Empty;
            foreach (DataColumn col in Table.Columns)
            {
                if (!string.IsNullOrEmpty(sFields)) sFields += ",";
                sFields += col.ColumnName;
            }

            return string.Format(MFSQLConst.cSelect, sFields, Table.TableName, string.Empty);
        }


        public string GetUpdateSQL()
        {
            string sFields=string.Empty;
            string sWhere = string.Empty;
            foreach (DataColumn col in Table.Columns)
            {
                if (!string.IsNullOrEmpty(sFields)) sFields += " , ";
                sFields +=string.Format("{0} = {1}",col.ColumnName,ParameterKey+col.ColumnName);
            }

            foreach (DataColumn col in Table.PrimaryKey)
            {
                if (!string.IsNullOrEmpty(sWhere)) sWhere += " and ";
                sWhere += string.Format("{0} = {1}", col.ColumnName, ParameterKey + OriginalKey + col.ColumnName);
            }

            return string.Format(MFSQLConst.cUpdate, Table.TableName, sFields, " Where " + sWhere);
            
        }

        public string GetDeleteSQL()
        {
            string sWhere = string.Empty;

            foreach (DataColumn col in Table.PrimaryKey)
            {
                if (!string.IsNullOrEmpty(sWhere)) sWhere += " and ";
                sWhere += string.Format("{0} = {1}", col.ColumnName, ParameterKey +OriginalKey+ col.ColumnName);
            }

            return string.Format(MFSQLConst.cDelect, Table.TableName, " Where " + sWhere);
        }

        public string GetInsertSQL()
        {
            string sFields = string.Empty;
            string sValue = string.Empty;
            foreach (DataColumn col in Table.Columns)
            {
                if (!string.IsNullOrEmpty(sFields)) sFields += " , ";
                sFields += col.ColumnName;

                if (!string.IsNullOrEmpty(sValue)) sValue += " , ";
                sValue += ParameterKey + col.ColumnName;
            }


            return string.Format(MFSQLConst.cInsert, Table.TableName, sFields, sValue);
        }

    }

    public class MFSQLConst
    {
        public const string cDelect = "Delete From {0} {1} ";
        public const string cUpdate = "Update {0} Set {1} {2} ";
        public const string cInsert = "Insert INTO {0}({1}) Values({2}) ";
        public const string cSelect = "Select {0} From {1} {2} ";

    }

}
